package com.lh.common.utils;

import com.lh.common.annotation.ExcelCheckNull;
import com.lh.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

/**
 * excel导入校验工具类
 * @author carl
 *
 */
@Slf4j
public class ExcelInportCheckUtils {

	/**
     * 通过反射来获取javaBean上的注解信息，判断属性值信息，然后通过注解元数据来返回
     */
//	public static <T> boolean doValidator(T clas){
//		Class<?> clazz = clas.getClass();
//		Field[] fields = clazz.getDeclaredFields();
//		for (Field field : fields) {
//			ExcelCheckNull checkNull = field.getDeclaredAnnotation(ExcelCheckNull.class);
//			if (null!=checkNull) {
//				Object value = getValue(clas, field.getName());
//				if (!notNull(value)) {
//					throwException(checkNull.message());
//				}
//			}
//		}
//		return true;
//	}

	/**
	 * 通过反射来获取javaBean上的注解信息，判断属性值信息，然后通过注解元数据来返回
	 */
	public static <T> String doValidator(T clas){
		Class<?> clazz = clas.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			ExcelCheckNull checkNull = field.getDeclaredAnnotation(ExcelCheckNull.class);
			if (null!=checkNull) {
				Object value = getValue(clas, field.getName());
				if (!notNull(value)) {
//					throwException(checkNull.message());
					return checkNull.message();
				}
			}
		}
		return null;
	}

	/**
	 * 获取当前fieldName对应的值
	 * @param clas		对应的bean对象
	 * @param fieldName	bean中对应的属性名称
	 * @return
	 */
	public static <T> Object getValue(T clas,String fieldName){
		Object value = null;
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(clas.getClass());
			PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : props) {
				if (fieldName.equals(property.getName())) {
					Method method = property.getReadMethod();
					value = method.invoke(clas, new Object[]{});
				}
			}
		} catch (Exception e) {
			log.error("errorInfo:{}",e);
		}
		return value;
	}

	/**
	 * 非空校验
	 * @param value
	 * @return
	 */
	public static boolean notNull(Object value){
		if(null==value){return false;}
        if(value instanceof String && isEmpty((String)value)){return false;}
        if(value instanceof List && isEmpty((List<?>)value)){return false;}
		if(value instanceof JSONArray && jsonArrayEmpty((JSONArray)value)){return false;}
		if(value instanceof Integer && isEmpty((Integer)value)){return false;}
		if(value instanceof BigDecimal && isEmpty((BigDecimal)value)){return false;}
		if(value instanceof Long && isEmpty((Long)value)){return false;}
        return null!=value;
	}

	public static boolean isEmpty(String str){return null==str || str.isEmpty() || str.trim().isEmpty();}
	public static boolean isEmpty(Integer str){return null==str;}
	public static boolean isEmpty(BigDecimal str){return null==str;}
	public static boolean isEmpty(Long str){return null==str;}
    public static boolean isEmpty(List<?> list){return null==list || list.isEmpty();}
	public static boolean jsonArrayEmpty(JSONArray jsonArray){return null==jsonArray || jsonArray.isEmpty();}

	private static void throwException(String msg) {
		if(null!=msg){
			throw new RRException(Constant.RESULT.FI1000,msg);
        }
	}

}
